{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Load all necessary packages\n",
    "import sys\n",
    "sys.path.append(\"../\")\n",
    "from collections import OrderedDict\n",
    "import json\n",
    "from pprint import pprint\n",
    "from aif360.datasets import GermanDataset\n",
    "from aif360.metrics import BinaryLabelDatasetMetric\n",
    "from aif360.explainers import MetricTextExplainer, MetricJSONExplainer\n",
    "from IPython.display import JSON, display_json"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Load dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "gd = GermanDataset()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Create metrics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "priv = [{'sex': 1}]\n",
    "unpriv = [{'sex': 0}]\n",
    "bldm = BinaryLabelDatasetMetric(gd, unprivileged_groups=unpriv, privileged_groups=priv)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Create explainers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "text_expl = MetricTextExplainer(bldm)\n",
    "json_expl = MetricJSONExplainer(bldm)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Text explanations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of positive-outcome instances: 700.0\n"
     ]
    }
   ],
   "source": [
    "print(text_expl.num_positives())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mean difference (mean label value on privileged instances - mean label value on unprivileged instances): -0.0748013090229\n"
     ]
    }
   ],
   "source": [
    "print(text_expl.mean_difference())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Disparate impact (probability of favorable outcome for unprivileged instances / probability of favorable outcome for privileged instances): 0.896567328205\n"
     ]
    }
   ],
   "source": [
    "print(text_expl.disparate_impact())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### JSON Explanations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_json(json_str):\n",
    "    return json.dumps(json.loads(json_str, object_pairs_hook=OrderedDict), indent=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"metric\": \"num_positives\", \n",
      "  \"message\": \"Number of positive-outcome instances: 700.0\", \n",
      "  \"numPositives\": 700.0, \n",
      "  \"description\": \"Computed as the number of positive instances for the given (privileged or unprivileged) group.\", \n",
      "  \"ideal\": \"The ideal value of this metric lies in the total number of positive instances made available\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "print(format_json(json_expl.num_positives()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"metric\": \"mean_difference\", \n",
      "  \"message\": \"Mean difference (mean label value on privileged instances - mean label value on unprivileged instances): -0.0748013090229\", \n",
      "  \"numPositivesUnprivileged\": 201.0, \n",
      "  \"numInstancesUnprivileged\": 310.0, \n",
      "  \"numPositivesPrivileged\": 499.0, \n",
      "  \"numInstancesPrivileged\": 690.0, \n",
      "  \"description\": \"Computed as the difference of the rate of favorable outcomes received by the unprivileged group to the privileged group.\", \n",
      "  \"ideal\": \"The ideal value of this metric is 0.0\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "print(format_json(json_expl.mean_difference()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"metric\": \"disparate_impact\", \n",
      "  \"message\": \"Disparate impact (probability of favorable outcome for unprivileged instances / probability of favorable outcome for privileged instances): 0.896567328205\", \n",
      "  \"numPositivePredictionsUnprivileged\": 201.0, \n",
      "  \"numUnprivileged\": 310.0, \n",
      "  \"numPositivePredictionsPrivileged\": 499.0, \n",
      "  \"numPrivileged\": 690.0, \n",
      "  \"description\": \"Computed as the ratio of likelihood of favorable outcome for the unprivileged group to that of the privileged group.\", \n",
      "  \"ideal\": \"The ideal value of this metric is 1.0\"\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "print(format_json(json_expl.disparate_impact()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
